/*
    Copyright (C) 2010-2011 AmigaVR
    
    This file is part of AmigaVR.
    Written by Ketfi-Chérif Mehdi <ketficherif.mehdi@gmail.com>

    AmigaVR is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AmigaVR is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AmigaVR.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "global.h"

void
 mod_load_info ( pmod_file mod )
{
	uint16_t i, j;
	for ( i = 0 ; i < MOD_SONG_TITLE_SIZE ; i++ )
		mod->title[i] = mread_byte ( MOD_SONG_TITLE+i );
	for ( i = 0; i < MOD_NSAMPLES ; i++ )
	{
		for ( j = 0; j < MOD_SAMPLE_NAME_SIZE ; j++ )
			mod->sample[i].name[j] = mread_byte ( mod_get_sampleinfo(i,MOD_SAMPLE_NAME+j) );
		mod->sample[i].length = mread_word ( mod_get_sampleinfo(i,MOD_SAMPLE_LENGTH) );
		mod->sample[i].finetune = mread_byte ( mod_get_sampleinfo(i,MOD_SAMPLE_FINETUNE) );
		mod->sample[i].volume = mread_byte ( mod_get_sampleinfo(i,MOD_SAMPLE_VOLUME) );
		mod->sample[i].repeat_offset = mread_word ( mod_get_sampleinfo(i,MOD_SAMPLE_REPEAT_OFFSET) );
		mod->sample[i].repeat_length = mread_word ( mod_get_sampleinfo(i,MOD_SAMPLE_REPEAT_LENGTH) );
	}
	mod->npatterns_played = mread_byte ( MOD_NPATTERNS );
	mod->jmp_end = mread_byte ( MOD_JMP_END );
	for ( i = 0; i < mod->npatterns_played; i++ )
		mod->pattern[i] = mread_byte ( MOD_PATTERN_TABLE+i );
	for ( i = 0; i < MOD_TAG_SIZE; i++ )
		mod->tag[i] = mread_byte ( MOD_TAG+i );
	for ( i = 0; i < MOD_BUFFER_MAX ; i++ )
		mod->buffer[i] = 0x00;
	mod->speed = 6;
}

uint8_t
 mod_is_supported ( pmod_file mod )
{
	if ( !strncmp ( mod->tag , "M.K." , 4 ) )
		return MOD_SUPPORTED;
	else if ( !strncmp ( mod->tag , "M!K!" , 4 ) )
		return MOD_SUPPORTED;
	else if ( !strncmp ( mod->tag , "M&K!" , 4 ) )
		return MOD_SUPPORTED;
	else if ( !strncmp ( mod->tag , "N.T." , 4 ) )
		return MOD_SUPPORTED;
	else if ( !strncmp ( mod->tag , "TDZ4" , 4 ) )
		return MOD_SUPPORTED;
	else if ( !strncmp ( mod->tag , "FA04" , 4 ) )
		return MOD_SUPPORTED;
	else if ( !strncmp ( mod->tag , "4CHN" , 4 ) )
		return MOD_SUPPORTED;
	else if ( !strncmp ( mod->tag , "FLT4" , 4 ) )
		return MOD_SUPPORTED;
	else
		return MOD_NOT_SUPPORTED;
}

void
 mod_get_npatterns ( pmod_file mod )
{
	uint8_t i;
	mod->npatterns = 0;
	for ( i = 0; i < MOD_MAX_PATTERNS; i++ )
	{
		if ( mod->pattern[i] > mod->npatterns )
			mod->npatterns = mod->pattern[i];
	}
	mod->npatterns++;
}

